package wnhz.com.business.shareshop.activity.classify;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.WindowManager;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.netcallback.DialogCallback;
import wnhz.com.business.shareshop.activity.classify.fragment.ClassifGoodsFragment;
import wnhz.com.business.shareshop.bean.ShareHomDataBean;
import wnhz.com.business.uitls.LogUtils;
import wnhz.com.business.uitls.ToastUtils;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.view.TranslucentActionBar;

/**
 * Created by Administrator on 2017-12-15 0015.
 * 商品种类(分类的底部的手机的所有手机的列表)
 */

public class ClassifPhonesActivity extends BaseActivity implements ActionBarClickListener {
    @BindView(R.id.tl_3)
        TabLayout tl_3;
    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.vp)
    ViewPager vp;

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private MyPagerAdapter mAdapter;
    private int position;
    private List<ShareHomDataBean.InfoBean.CateListBean> cate_list;


    public static Intent createIntent(Context context,int position) {
        Intent intent = new Intent(context, ClassifPhonesActivity.class);
        intent.putExtra("position",position);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classify_goods);
        ButterKnife.bind(this);
        position = getIntent().getIntExtra("position", 0);
        if (null != getIntent().getStringExtra("title")) {
            actionbar.setData(getIntent().getStringExtra("title"), R.drawable.ic_lefft_white, null, 0, null, 0, this);
        }else {
            actionbar.setData("手机", R.drawable.ic_lefft_white, null, 0, null, 0, this);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }
       getHomFragmentData();
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }
    public void getHomFragmentData() {
        Map<String, String> map = new HashMap<>();
        map.put("token", MyApplication.getToken());
//        map.put("cateId", "1");
        map.put("page", "0");
        map.put("goods_type", "4");

        OkGo.<ShareHomDataBean>post(Url.HOME_INDEXDADA).tag(this).params(map, false).execute(new DialogCallback<ShareHomDataBean>(ClassifPhonesActivity.this) {
            @Override
            public void onSuccess(Response<ShareHomDataBean> response) {

                if (response.body().getRe().equals("1")) {
                    LogUtils.e("wxf", "共享商城" + response.body().toString());
                    cate_list = response.body().getInfo().getCate_list();
                    initType(cate_list);
                } else {
                    ToastUtils.showToast(ClassifPhonesActivity.this, response.body().getRe());
                }


            }
        });

    }

    private void initType(List<ShareHomDataBean.InfoBean.CateListBean> cate_list) {
        if (cate_list != null) {
            for (int i = 0; i < cate_list.size(); i++) {
                tl_3.addTab(tl_3.newTab().setText(cate_list.get(i).getClassname()));
                ClassifGoodsFragment instance = ClassifGoodsFragment.getInstance();
                Bundle args = new Bundle();
                args.putString("cate_id",cate_list.get(i).getCate_id());
                instance.setArguments(args);
                mFragments.add(instance);
            }
        }
        mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        vp.setAdapter(mAdapter);
        tl_3.setupWithViewPager(vp);
        tl_3.setScrollPosition(position,0,true);
        vp.setCurrentItem(position);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return cate_list.get(position).getClassname();
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}
